namespace SinaFinance.SummaryInfoLibrary
{
    using SinaFinance.CtrlDelegate;
    using SinaFinanceBaseClassLibrary;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Net.NetworkInformation;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Linq;

    public partial class TopStockView : UserControl
    {
        //private bool _contentLoaded;
        private XDocument doc;
        private DB_SinaDataHelp Dsdh;
        private readonly Color greenColor = Color.FromArgb(0xff, 0x71, 0x8d, 5);
        //internal Canvas LayoutRoot;
        public bool LoadLocalFileSuccessfully = false;
        private EventHandler queryMarketInfo;
        private readonly Color redColor = Color.FromArgb(0xff, 0xff, 0, 0);
        private string stockCode;
        private string stockName = string.Empty;
        private string stockNum = string.Empty;
        //internal TextBlock txtChangePercent;
        //internal TextBlock txtChangePercent_;
        //internal TextBlock txtStockName;
        //internal TextBlock txtStockName_;
        //internal TextBlock txtTrade;
        //internal TextBlock txtTrade_;

        public event EventHandler QueryMarketInfo
        {
            [MethodImpl(MethodImplOptions.Synchronized)] add
            {
                this.queryMarketInfo = (EventHandler)Delegate.Combine((Delegate)this.queryMarketInfo, (Delegate)value);
            }
            [MethodImpl(MethodImplOptions.Synchronized)] remove
            {
                this.queryMarketInfo = (EventHandler)Delegate.Remove((Delegate)this.queryMarketInfo, (Delegate)value);
            }
        }

        public TopStockView()
        {
            this.InitializeComponent();
            base.Loaded += new RoutedEventHandler(this.StockBasicInfo_Loaded);
            this.doc = XDocument.Load("PageConfig.xml");
        }

        private void Dsdh_GetTop2BaseDataCompleted(List<DB_BaseInfo> _DB_BaseInfo, string result)
        {
            if ((_DB_BaseInfo != null) && _DB_BaseInfo.Count.Equals(2))
            {
                XElement element = (from labels in this.doc.Elements("Configs").Elements<XElement>("BaseInfoArea").Elements<XElement>("BaseInfoLabel") select labels).ToList<XElement>()[0];
                this.txtStockName.Text = _DB_BaseInfo[0].Name;
                this.txtStockName.Tag = _DB_BaseInfo[0].Symbol;
                this.txtTrade.Text = _DB_BaseInfo[0].Trade;
                this.txtChangePercent.Text = Convert.ToDouble(_DB_BaseInfo[0].ChangePercent).ToString("0.00") + element.Element("ChangePercentUnit").Value;
                this.txtStockName_.Text = _DB_BaseInfo[1].Name;
                this.txtStockName_.Tag = _DB_BaseInfo[1].Symbol;
                this.txtTrade_.Text = _DB_BaseInfo[1].Trade;
                this.txtChangePercent_.Text = Convert.ToDouble(_DB_BaseInfo[1].ChangePercent).ToString("0.00") + element.Element("ChangePercentUnit").Value;
                if (Convert.ToDouble(_DB_BaseInfo[0].ChangePercent) >= 0.0)
                {
                    this.txtChangePercent.Foreground = new SolidColorBrush(this.redColor);
                }
                else
                {
                    this.txtChangePercent.Foreground = new SolidColorBrush(this.greenColor);
                }
                if (Convert.ToDouble(_DB_BaseInfo[1].ChangePercent) >= 0.0)
                {
                    this.txtChangePercent_.Foreground = new SolidColorBrush(this.redColor);
                }
                else
                {
                    this.txtChangePercent_.Foreground = new SolidColorBrush(this.greenColor);
                }
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    LocalFileStorage.CreateStockMarketBasicInfoFile(result);
                    LocalFileStorage.CreateMarketDetailInfoFile(result);
                }
            }
        }

        public void GetBaseInfo(string codes)
        {
            if (!string.IsNullOrEmpty(codes))
            {
                this.stockCode = codes;
                if (this.Dsdh == null)
                {
                    this.Dsdh = new DB_SinaDataHelp();
                    this.Dsdh.GetTop2BaseDataCompleted += new GetTop2BaseDataHelp(this.Dsdh_GetTop2BaseDataCompleted);
                }
                this.Dsdh.GetTop2StockInfoData(this.stockCode);
            }
        }

        private void GetTop2TxtBaseDataCompleted(List<DB_BaseInfo> _DB_BaseInfo)
        {
            if ((_DB_BaseInfo != null) && !_DB_BaseInfo.Count.Equals(0))
            {
                XElement element = (from labels in this.doc.Elements("Configs").Elements<XElement>("BaseInfoArea").Elements<XElement>("BaseInfoLabel") select labels).ToList<XElement>()[0];
                this.txtStockName.Text = _DB_BaseInfo[0].Name;
                this.txtStockName.Tag = _DB_BaseInfo[0].Symbol;
                this.txtTrade.Text = Convert.ToDouble(_DB_BaseInfo[0].Trade).ToString("0.00");
                this.txtChangePercent.Text = _DB_BaseInfo[0].ChangePercent + element.Element("ChangePercentUnit").Value;
                this.txtStockName_.Text = _DB_BaseInfo[1].Name;
                this.txtStockName_.Tag = _DB_BaseInfo[1].Symbol;
                this.txtTrade_.Text = Convert.ToDouble(_DB_BaseInfo[1].Trade).ToString("0.00");
                this.txtChangePercent_.Text = _DB_BaseInfo[1].ChangePercent + element.Element("ChangePercentUnit").Value;
                if (Convert.ToDouble(_DB_BaseInfo[0].ChangePercent) >= 0.0)
                {
                    this.txtChangePercent.Foreground = new SolidColorBrush(this.redColor);
                }
                else
                {
                    this.txtChangePercent.Foreground = new SolidColorBrush(this.greenColor);
                }
                if (Convert.ToDouble(_DB_BaseInfo[1].ChangePercent) >= 0.0)
                {
                    this.txtChangePercent_.Foreground = new SolidColorBrush(this.redColor);
                }
                else
                {
                    this.txtChangePercent_.Foreground = new SolidColorBrush(this.greenColor);
                }
                this.LoadLocalFileSuccessfully = true;
            }
        }

        //[DebuggerNonUserCode]
        //public void InitializeComponent()
        //{
        //    if (!this._contentLoaded)
        //    {
        //        this._contentLoaded = true;
        //        Application.LoadComponent(this, new Uri("/SinaFinance;component/SummaryInfoLibrary/TopStockView.xaml", UriKind.Relative));
        //        this.LayoutRoot = (Canvas) base.FindName("LayoutRoot");
        //        this.txtStockName = (TextBlock) base.FindName("txtStockName");
        //        this.txtTrade = (TextBlock) base.FindName("txtTrade");
        //        this.txtChangePercent = (TextBlock) base.FindName("txtChangePercent");
        //        this.txtStockName_ = (TextBlock) base.FindName("txtStockName_");
        //        this.txtTrade_ = (TextBlock) base.FindName("txtTrade_");
        //        this.txtChangePercent_ = (TextBlock) base.FindName("txtChangePercent_");
        //    }
        //}

        public void LoadLocalFile()
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                this.GetTop2TxtBaseDataCompleted(LocalFileStorage.ReadMarketInfoFile());
            }
        }

        private void StockBasicInfo_Loaded(object sender, RoutedEventArgs e)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                IntervalUpdates.SetDelegate(new SinaFinance.CtrlDelegate.UpdateBaseInfoDelegate(this.UpdateBaseInfoByInterval));
                IntervalUpdates.Start();
            }
            this.txtStockName.MouseLeftButtonDown += new MouseButtonEventHandler(this.txtFirstStock_MouseLeftButtonDown);
            this.txtStockName_.MouseLeftButtonDown += new MouseButtonEventHandler(this.txtSecondStock_MouseLeftButtonDown);
        }

        private void txtFirstStock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.queryMarketInfo != null)
            {
                this.queryMarketInfo(sender, e);
            }
        }

        private void txtSecondStock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.queryMarketInfo != null)
            {
                this.queryMarketInfo(sender, e);
            }
        }

        public void UpdateBaseInfoByInterval()
        {
            if (!string.IsNullOrEmpty(this.stockCode))
            {
                if (this.Dsdh == null)
                {
                    this.Dsdh = new DB_SinaDataHelp();
                    this.Dsdh.GetTop2BaseDataCompleted += new GetTop2BaseDataHelp(this.Dsdh_GetTop2BaseDataCompleted);
                }
                this.Dsdh.GetTop2StockInfoData(this.stockCode);
            }
        }

        public delegate void EventHandler(object sender, MouseEventArgs e);

        public delegate void UpdateBaseInfoDelegate();
    }
}
